<?php

namespace App\Service\Office;

use App\Models\Knowledge;
use App\Models\LitQuestion;
use App\Models\Qh_LitQuestion;
use DB;
use Exception;
use Illuminate\Support\Facades\Log;

class PaperImportService
{
    public string $imgPath;

    /**
     * 题目入库，返回模型
     * @param $question
     * @param $courseId
     * @param $province
     * @param $city
     * @param bool $latexToPng
     * @param null $thirdKnowledge
     * @return array
     * @throws Exception
     */
    public function saveQuestionToDatabase($question, $courseId, $province, $city, bool $latexToPng=false, $thirdKnowledge=null): array
    {
        try {
            $q = importQuestionFormat($question, $courseId, $province, $city, $this->imgPath, $latexToPng);

            $firstKnowledgeId = null;
            $secondKnowledgeId = null;
            $thirdKnowledgeId = null;

            if (!is_null($thirdKnowledge)) {
                $secondKnowledge = Knowledge::find($thirdKnowledge->parentId);
                $thirdKnowledgeId = $thirdKnowledge->id;    // 3
                if ($secondKnowledge) {
                    $firstKnowledgeId = $secondKnowledge->parentId;  // 1
                    $secondKnowledgeId = $thirdKnowledge->parentId; // 2

                    $q['secondKnowledgeId'] = $secondKnowledgeId;
                    $q['firstKnowledgeId'] = $firstKnowledgeId;
                }
                $q['thirdKnowledgeId'] = $thirdKnowledgeId;
            }
            $qId = DB::table('tiku_questions')->insertGetId($q);

            // 小题入库
            if ($qId && !empty($question['children'])) {
                foreach ($question['children'] as $childQuestion) {
                    $childQuestion['knowledge'] = $question['knowledge'];
                    $childQuestion = importQuestionFormat($childQuestion, $courseId, $province, $city, $this->imgPath, $latexToPng);
                    $childQuestion['parentId'] = $qId;

                    if (!is_null($thirdKnowledge)) {
                        $childQuestion['firstKnowledgeId'] = $firstKnowledgeId ?? 0;
                        $childQuestion['secondKnowledgeId'] = $secondKnowledgeId ?? 0;
                        $childQuestion['thirdKnowledgeId'] = $thirdKnowledgeId;
                    }

                    $_litQ = LitQuestion::create($childQuestion);
                    if (!$_litQ) throw new Exception('小题导入失败');
                }
            }
        } catch (Exception $e) {
            Log::error('[试卷导入]'. $e->getMessage());
            Log::error('[试卷导入Exception]', (array)$e);
            throw new Exception('导入失败');
        }

        return [
            'id' => $qId,
            'questionTypeId' => $q['questionTypeId']
        ];
    }

    /**
     * 题目入库，返回模型
     * @param $question
     * @param $courseId
     * @param $province
     * @param $city
     * @param bool $latexToPng
     * @return array
     * @throws Exception
     */
    public function saveQuestionToDatabaseByQiHang($question, $courseId, $province, $city, bool $latexToPng=false): array
    {
        try {
            $q = importQuestionFormatByQiHang($question, $courseId, $province, $city, $this->imgPath, $latexToPng);

            $qId = DB::table('qh_questions')->insertGetId($q);

            // 小题入库
            if ($qId && !empty($question['children'])) {
                foreach ($question['children'] as $childQuestion) {
                    $childQuestion = importQuestionFormatByQiHang($childQuestion, $courseId, $province, $city, $this->imgPath, $latexToPng);
                    $childQuestion['parentId'] = $qId;
                    $_litQ = Qh_LitQuestion::create($childQuestion);
                    if (!$_litQ) throw new Exception('小题导入失败');
                }
            }
        } catch (Exception $e) {
            Log::error('[试卷导入]'. $e->getMessage());
            Log::error('[试卷导入Exception]', (array)$e);
            throw new Exception('导入失败');
        }

        return [
            'id' => $qId,
            'questionTypeId' => $q['questionTypeId']
        ];
    }
}
